/**
 * table操作栏工具
 */
import React from 'react';
import { Button } from 'antd';
import WithElementAuth from '@/components/WithElementAuth';
import styles from './index.less';

interface TableToolProps {
  options: any[];
  [propName: string]: any;
}

class Links extends React.Component {
  state = {};

  render() {
    // eslint-disable-next-line jsx-a11y/anchor-has-content
    return <a className={styles['table-tool__link']} {...this.props} />;
  }
}

export const WithLinkAuth = WithElementAuth(Links);
export const WithButtonAuth = WithElementAuth(Button);

class TableTool extends React.PureComponent<TableToolProps> {
  renderTableToolView() {
    const { options } = this.props;
    return options.map((item: any) => {
      const { event = {}, name, auth } = item;
      if (item.default && item.default === 'button') {
        return (
          <WithButtonAuth
            type="primary"
            size="default"
            key={name}
            auth={auth}
            onClick={event.click}
            {...item}
          >
            {name}
          </WithButtonAuth>
        );
      }
      return (
        <WithLinkAuth key={name} auth={auth} onClick={event.click} {...item}>
          {name}
        </WithLinkAuth>
      );
    });
  }

  render() {
    const { isCenter } = this.props;
    return (
      <div className={styles['table-tool']} style={{ textAlign: isCenter ? 'center' : 'right' }}>
        {this.renderTableToolView()}
      </div>
    );
  }
}

export default TableTool;
